package test
{
	import control.micro.AttackingAIBattleController;
	import control.micro.DefendingHumanBattleController;
	import events.BattleEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import model.macro.Armor;
	import model.macro.Fortress;
	import model.macro.FortressType;
	import model.macro.Player;
	import model.macro.RangedWeapon;
	import model.macro.Territory;
	import model.macro.Unit;
	import model.macro.Weapon;
	import model.micro.Battle;
	import util.Random;
	import util.Settings;
	import view.FrameRateCounter;
	import view.micro.BattleSprite;
	import view.microgui.HUDSprite;
	import view.microgui.SideHUDSprite;
	
	/**
	 * Used for testing functionality of the micro test.
	 */
	public class MicroTest extends Sprite
	{
		private var battle:Battle;
		private var battle_sprite:BattleSprite;
		private var frame_rate_counter:FrameRateCounter;
		private var defender_player_battle_controller:DefendingHumanBattleController;
		private var attacker_ai_battle_controller:AttackingAIBattleController;
		private var hud:HUDSprite;
		private var sideHud:SideHUDSprite;
		
		/**
		 * Create a new test.
		 */
		public function MicroTest()
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.ENTER_FRAME, update);
		}
		
		/**
		 * Called when added to a stage.
		 * @param	e	AddedToStage event
		 */
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			var player1:Player = new Player("simon", 0x0000FF);
			var player2:Player = new Player("compy", 0x00FF00);
			
			var territory1:Territory = new Territory("home");
			territory1.owner = player1;
			var territory2:Territory = new Territory("invading");
			territory2.owner = player2;

			// model
			var attackers:Vector.<Unit> = new Vector.<Unit>();
			var defenders:Vector.<Unit> = new Vector.<Unit>();
			var fortress:Fortress = new Fortress(FortressType.CASTLE);
			
			var weapons:Array = [Weapon.HALBERD, Weapon.SWORD, RangedWeapon.LONGBOW];
			for (var i:int = 0; i < 10; i++)
			{
				attackers.push(new Unit(player2, Random.choose(weapons) as Weapon, Armor.CLOTH));
			}
			for (i = 0; i < 15; i++)
			{
				defenders.push(new Unit(player1, Random.choose([Weapon.SWORD, Weapon.HALBERD]) as Weapon, Armor.CLOTH));
			}
			for (i = 0; i < 3; i++)
			{
				defenders.push(new Unit(player1, RangedWeapon.LONGBOW, Armor.CLOTH));
			}
			
			battle = new Battle(attackers, defenders, fortress, territory1, territory2);
			
			battle.addEventListener(BattleEvent.BATTLE_ENDED, battleEnded);
			
			// views
			battle_sprite = new BattleSprite(battle);
			addChild(battle_sprite);
			frame_rate_counter = new FrameRateCounter();
			frame_rate_counter.x = stage.stageWidth - frame_rate_counter.width;
			addChild(frame_rate_counter);
			hud = new HUDSprite(battle);
			addChild(hud);
			
			battle_sprite.y = stage.stageHeight;
			
			// controllers
			defender_player_battle_controller = new DefendingHumanBattleController(battle, stage);
			attacker_ai_battle_controller = new AttackingAIBattleController(battle);
		
		}
		
		private function battleEnded(e:BattleEvent):void
		{
			battle = null;
			var text:TextField = new TextField;
			text.defaultTextFormat = new TextFormat("Arial", 80, 0xFFFFFF, null, null, null, null, null, TextFormatAlign.CENTER);
			text.text = "BATTLE OVER";
			text.width = stage.stageWidth;
			addChild(text);
		}
		
		/**
		 * Called every frame.
		 * @param	e	EnterFrame event
		 */
		private function update(e:Event):void
		{
			if (battle != null)
			{
				battle.update();
				battle_sprite.update();
				hud.update();
				defender_player_battle_controller.update();
				attacker_ai_battle_controller.update();
			}
		}
	}

}